package com.hua.util;

import com.hua.common.Response;
import com.hua.common.ResponseCode;
import com.hua.domain.domainVo.CategoryVo;
import com.hua.exception.BusinessException;
import com.hua.minio.service.MinioService;
import com.hua.service.impl.CategoryServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Objects;
import java.util.UUID;

/**
 * ClassName:FaceClass
 * Package:com.hua.util
 * Description:
 *
 * @data: 2022/9/26 16:48
 * @author:ChenJH
 */
@Component
public class FaceClass {
    @Resource
    private MinioService minioService;

    @Resource
    private RedisCache redisCache;

    @Resource
    private CategoryServiceImpl categoryService;

    public Response<String> uploadFace(MultipartFile file) {
        if(Objects.isNull(file) || file.getSize()==0){
            throw new BusinessException(ResponseCode.SYSTEM_ERROR);
        }
        String fileName = file.getOriginalFilename();
//        System.out.println(fileName);
        String newName = null;
        if (!Objects.isNull(file)) {
            newName = "image/" + UUID.randomUUID().toString().replaceAll("-", "")
                    + fileName.substring(fileName.lastIndexOf("."));
        }
        InputStream inputStream = null;
        String url = null;
        try {
            inputStream = file.getInputStream();
            //上传文件到Minio
            minioService.uploadFile(inputStream, newName, file.getContentType());
            url = minioService.getFilePermanentUrl(newName);
            redisCache.setCacheObject("url",url);
//            String oldName = categoryService.getById(redisCache.getCacheObject("userId")).getCategoryImg();
//            redisCache.setCacheObject("oldName",oldName);
            redisCache.setCacheObject("newName",newName);
            redisCache.setCacheObject("isUpload","true");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResponseCode.SYSTEM_ERROR);
        }
        return ResultUtils.success(url);
    }

    public Response<String> saveFace(CategoryVo categoryVo) {
        String url = redisCache.getCacheObject("url");
//        String oldName = redisCache.getCacheObject("oldName");
        Long userId = redisCache.getCacheObject("userId");
        if(Objects.isNull(url)){
            url = categoryService.getById(userId).getCategoryImg();
//            oldName = categoryService.getById(userId).getCategoryImg();
            if(Objects.isNull(url)){
                throw new BusinessException(ResponseCode.SYSTEM_ERROR);
            }
        }
        if (!Objects.isNull(url)) {
            String newName = redisCache.getCacheObject("newName");
            if (categoryService.updateCurrentOfUserFace(url, newName,categoryVo) > 0) {
                try {
                    //此值如果是null 则证明没有进入upload方法，所有头像没变，不用删除，也不用修改
                    redisCache.deleteObject("url");
                    redisCache.deleteObject("newName");
                    redisCache.deleteObject("isUpload");
                } catch (Exception e) {
                    throw new BusinessException(ResponseCode.SYSTEM_ERROR);
                }
                redisCache.deleteObject("categories");
                return ResultUtils.success("添加成功");
            } else {
                throw new BusinessException(ResponseCode.SYSTEM_ERROR);
            }
        }
        throw new BusinessException(ResponseCode.SYSTEM_ERROR);
    }
}
